﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication3

{
    using Reports;
    using Reports.companypaymentdatasetTableAdapters;
    public partial class CompanyPaymentForm : Form
    {
        private String connString = Program.ConnStr;
        Transaction.CompanyPaymentTransaction companytransaction = null;
        DataBean.CompanyPaymentDatabean companyPaymentbean = null;



        public CompanyPaymentForm()
        {

            InitializeComponent();
           // newinvoiceget();
            companytransaction = new Transaction.CompanyPaymentTransaction();
            lblinvoice.Text = companytransaction.newinvoiceget();
        }


      








        /// <summary>
        /// fill the jobcode to combobox
        /// </summary>

        public void jobcodecomboboxload()
        {

            OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
            oleDbConnection1.Open();
            OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select jobpk,jobcode,customerpk,customercode,companyName from  jobcodemastertable", oleDbConnection1);
            OleDbDataReader reader = oleDbCommand1.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Columns.Add("jobpk", typeof(int));
            dt.Columns.Add("jobcode", typeof(string));
            dt.Columns.Add("customerpk", typeof(int));
            dt.Columns.Add("customercode", typeof(string));
            dt.Columns.Add("companyName", typeof(string));
            dt.Load(reader);
            cmbjobcode.ValueMember = "jobpk";
            cmbjobcode.DisplayMember = "jobcode";
            txtcustcode.Text = dt.Rows[0]["customercode"].ToString();
            txtcustname.Text = dt.Rows[0]["companyName"].ToString();
            lblcustpk.Text = dt.Rows[0]["customerpk"].ToString();
            cmbjobcode.DataSource = dt.DefaultView;


            oleDbConnection1.Close();

        }


        /// <summary>
        /// for fillingthe customerdetails
        /// </summary>
        /// 
        public void customerdetailsfill()
        {
            OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
            oleDbConnection1.Open();
            OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select jobpk,jobcode,customerpk,customercode,companyName from  jobcodemastertable where jobpk=  @jobpk ", oleDbConnection1);
            oleDbCommand1.Parameters.AddWithValue("@jobpk", cmbjobcode.SelectedValue);
            OleDbDataReader reader = oleDbCommand1.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Columns.Add("jobpk", typeof(int));
            dt.Columns.Add("jobcode", typeof(string));
            dt.Columns.Add("customerpk", typeof(int));
            dt.Columns.Add("customercode", typeof(string));
            dt.Columns.Add("companyName", typeof(string));
            dt.Load(reader);
          
            txtcustcode.Text = dt.Rows[0]["customercode"].ToString();
            txtcustname.Text = dt.Rows[0]["companyName"].ToString();
            lblcustpk.Text = dt.Rows[0]["customerpk"].ToString();
          


            oleDbConnection1.Close();

        }
        /// <summary>
        /// fill the confirmed quotation against each jobcode to the quotationcobobox
        /// </summary>
        public void comboQuotationboxload()
        {

            OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
            oleDbConnection1.Open();

            OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select confirmpk ,quotationcode , amount from  confirmquotationmastertable  where jobpk = " + cmbjobcode.SelectedValue + "  ", oleDbConnection1);



            OleDbDataReader reader = oleDbCommand1.ExecuteReader();


            DataTable dt = new DataTable();

            dt.Columns.Add("confirmpk", typeof(int));
            dt.Columns.Add("quotationcode", typeof(string));
            dt.Columns.Add("amount", typeof(int));
            dt.Load(reader);
            if (dt.Rows.Count != 0)
            {
                cmbQuotationcode.ValueMember = "confirmpk";
                cmbQuotationcode.DisplayMember = "quotationcode";
                cmbQuotationcode.DataSource = dt.DefaultView;
              //  lbltotalamount.Text = dt.Rows[0]["amount"].ToString();



                oleDbConnection1.Close();
            }
            else
            {
                MessageBox.Show("NO QUOTATION OF SAME");
                cmbQuotationcode.Text = "";

            }

        }





        private void CompanyPaymentForm_Load(object sender, EventArgs e)
        {
            jobcodecomboboxload();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// when jobcode index changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbjobcode_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboQuotationboxload();
            customerdetailsfill();
           

        }
        /// <summary>
        /// if user click radiobutton for Cash payment
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rbtCash_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtcheque.Checked)
            {
                txtBankName.Enabled = true;
                txtchequeno.Enabled = true;
                dtpchequedate.Enabled = true;
                dtpreleasingdate.Enabled = true;

            }
            else
            {
                txtBankName.Enabled = false;
                txtchequeno.Enabled = false;
                dtpchequedate.Enabled = false;
                dtpreleasingdate.Enabled = false;
            }
        }




        /// <summary>
        /// VALIDATES ALL CONTROL
        /// </summary>
        /// <returns></returns>
        public Boolean validationcontrol()
        {
            Boolean sucess = true;

            if (lblcustpk.Text.Trim() == null || lblcustpk.Text.Trim() == "" || lblcustpk.Text.Trim() == "pk" || lblcustpk.Text.Trim() == "###")
            {
                MessageBox.Show("NO customer data found");
                sucess = false;
                this.Close();
            }
            if (cmbjobcode.Text.Trim() == null || cmbjobcode.Text.Trim() == "" || cmbjobcode.Text.Trim() == "pk")
            {
                MessageBox.Show("NO jOBCODE data found");
                sucess = false;
                this.Close();
            }

            if (mxtAmount.Text.Trim() == null || mxtAmount.Text.Trim() == "")
            {

                mxtAmount.Focus();
                mxtAmount.Text = "";
                sucess = false;
                lblstatus.Text = "Enter recieved Amount ";
            }
            else if (rbtcheque.Checked)
            {
                if (txtchequeno.Text.Trim() == null || txtchequeno.Text.Trim() == "")
                {

                    txtchequeno.Focus();
                    txtchequeno.Text = "";
                    sucess = false;
                    lblstatus.Text = "Enter Cheque No";
                }
                if (txtBankName.Text.Trim() == null || txtBankName.Text.Trim() == "")
                {

                    txtBankName.Focus();
                    txtBankName.Text = "";
                    sucess = false;
                    lblstatus.Text = "Enter Bank Name";
                }


            }




            return sucess;
        }





        /// <summary>
        /// transfer data to a databean 
        /// </summary>
        /// <returns></returns>
        public void transferDataBean()
        {
            companyPaymentbean = new DataBean.CompanyPaymentDatabean();
            companyPaymentbean.Jobcode = cmbjobcode.Text;
            companyPaymentbean.Jobpk = int.Parse(cmbjobcode.SelectedValue.ToString());
            companyPaymentbean.Confirmpk = int.Parse(cmbQuotationcode.SelectedValue.ToString());
            companyPaymentbean.QuotationCode = cmbQuotationcode.Text;
            /////////////////////////////
            companyPaymentbean.Quotationpk = 1;
            ////////////////////////////////
            companyPaymentbean.Customername = txtcustname.Text;
            companyPaymentbean.Customercode = txtcustcode.Text;
            companyPaymentbean.Customerpk = int.Parse(lblcustpk.Text.ToString());
            companyPaymentbean.Paymentdate = DateTime.Now.Date;
            companyPaymentbean.Cmpinvoice = lblinvoice.Text;
            companyPaymentbean.Amount = int.Parse(mxtAmount.Text);


            if (rbtcheque.Checked)
            {
                companyPaymentbean.Chequereleasedate = dtpreleasingdate.Value.Date;
                companyPaymentbean.Chequeno = txtchequeno.Text;
                companyPaymentbean.BankName = txtBankName.Text;
                companyPaymentbean.Chequedate = dtpchequedate.Value.Date;

                companyPaymentbean.Iscash = false;
                companyPaymentbean.Ischeque = true;
            }
            else if (rbtCash.Checked)
            {
                companyPaymentbean.Chequeno = "";
                companyPaymentbean.BankName = "";
                companyPaymentbean.Iscash = true;
                companyPaymentbean.Ischeque = false;

            }


            try
            {
                companytransaction.insertpaymentintodb(companyPaymentbean);
                MessageBox.Show(lblinvoice.Text);
                this.Close();
            }
            catch (Exception exp)
            {
                
                MessageBox.Show("unable to insert datato database>> at companytransaction.insertpaymentintodb(companyPaymentbean) "+exp+"");
                this.Close();
            }

            
        

        }



        /// <summary>
        /// when data is filled and btnComplete added
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnComplete_Click(object sender, EventArgs e)
        {
            transferDataBean();


        }

        private void cmbQuotationcode_SelectedIndexChanged(object sender, EventArgs e)
        {
            fillpaymenttable();
            lbltotalamount.Text = companytransaction.getamountbyagreedquotation(int.Parse(cmbQuotationcode.SelectedValue.ToString())).ToString();
            addtotalpaid();
           
        }



        public void fillpaymenttable()
        {

            Companypaymenttableadapter adapt = new Companypaymenttableadapter();

            adapt.Connection = new OleDbConnection(Program.ConnStr);//This is not mandatory.just to confirm.
           
         DataTable dt = adapt.GetDataBypk(int.Parse(cmbQuotationcode.SelectedValue.ToString()), int.Parse(cmbjobcode.SelectedValue.ToString()));
       
         tblpaymentview.DataSource = dt;
            

        }


        public void calcutateall()
        {

        }






        /// <summary>
        ///ADD ALL PAID AMOUNT AND DISPLAY IN A LABEL
        ///add all previously paid amount and gives the total paid
        /// </summary>
        public void addtotalpaid()
        {

            try
            {
                double sum = 0;
                double balance = 0;
                double amounttoget = double.Parse(lbltotalamount.Text.ToString());

                int count = tblpaymentview.RowCount;
                if (count != 0)
                {

                    for (int i = 0; i < count; i++)
                    {

                        if (tblpaymentview.Rows[i].Cells[3].Value != null)
                        {
                            sum = sum + double.Parse(tblpaymentview.Rows[i].Cells[5].Value.ToString());
                        }
                    }
                    balance = amounttoget - sum;



                lblreceived.Text = sum.ToString();
                    lblbalance.Text = balance.ToString();

                }
            }
            catch (Exception exp)
            {

                MessageBox.Show("Error  Calculating totalpaid >>>>at driverpaymentform. addtotalpaid()" + exp + "");
                this.Close();
            }






        }












/*


        /// <summary>
        /// to getnew jobcode autogenerated
        /// </summary>
        public void newinvoiceget()
        {
            try
            {
                String invoiceno = null;
                Int32 S = 0;
                lblinvoice.Text = "";
                OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
                oleDbConnection1.Open();


                String query = "SELECT TOP 1 paymentpk FROM companypaymentmastertable ORDER BY paymentpk DESC";
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);


                DataSet ds = new DataSet();

                dAdapter.Fill(ds, "tblpaymentpk");
                if (ds.Tables[0].Rows.Count == 0)
                {
                    S = 0;
                }
                else
                {
                    S = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                }

                S++;

                invoiceno = "INV00" + S.ToString();

                lblinvoice.Text = "CINV#0" + S.ToString();
                lblinvoice.Enabled = false;
            }
            catch (Exception exp)
            {

                MessageBox.Show("Failed to autogenerate INVOICE ");
                MessageBox.Show(exp.ToString());
                this.Close();
            }

        }

*/







    }

}